﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RoleModuleFrm.aspx.cs"
    Inherits="WebApp.RoleModuleFrm" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>角色菜单授权</title>
    <link rel="stylesheet" type="text/css" href="css/newStyle.css" />
    <link rel="stylesheet" type="text/css" href="js/jquery-easyui-1.3.2/themes/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="js/jquery-easyui-1.3.2/themes/icon.css" />

    <script type="text/javascript" src="js/common.js"></script>

    <script type="text/javascript" src="js/jquery-easyui-1.3.2/jquery-1.8.0.min.js"></script>

    <script type="text/javascript" src="js/jquery-easyui-1.3.2/jquery.easyui.min.js"></script>

    <script type="text/javascript" src="js/jquery-easyui-1.3.2/locale/easyui-lang-zh_CN.js"></script>

    <script type="text/javascript">
        $(document).ready(function() {
            initData();
        });

        function initData() {
            $.getJSON("JSONHandler.ashx?DataType=SysRoleType", function(result) {
                $('#eRole').combobox('loadData', result);
            });
        }

        function bindMenus(roleId) {
            if (roleId) {
                WebApp.RoleModuleFrm.GetMenus(roleId, function callback(p) {
                    if (p.error) {
                        $.messager.alert('异常消息', '获取数据异常,' + p.error.Message, 'error');
                        return;
                    }

                    var res = p.value;
                    if (res) {
                        var data = res.Data;
                        var msg = res.Msg;
                        if (msg)
                            $.messager.alert('信息获取异常', msg, 'error');
                        else {
                            if (data) {
                                $('#sLeft').html(data[0]);
                                $('#sRight').html(data[1]);
                            }
                        }
                    }
                });
            }
        }

        function onRoleSelect(rec) {
            bindMenus(rec.Value);
        }

        function moveListItem(lsFrom, lsTo) {
            var size = $("#" + lsFrom + " option").size();
            var selsize = $("#" + lsFrom + " option:selected").size();
            if (size > 0 && selsize > 0) {
                $.each($("#" + lsFrom + " option:selected"), function(i, own) {
                    //                    var sText = $(own).text();
                    //                    var sValue = $(own).val();
                    //                    $("#" + lsTo).append("<option value=\"" + sValue + "\">" + sText + "</option>"); 
                    //                    $(own).remove(); 
                    $(own).appendTo($("#" + lsTo));
                    $("#" + lsFrom + "").children("option:first").attr("selected", true);
                });
            }
        }

        function saveData() {
            var validate = $('#fm').form('validate');
            if (!validate)
                return;

            var roleId = $('#eRole').combobox('getValue');
            
            var items = new Array();
            $("#sRight option").each(function() {
                items.push(this.value);
            });

            WebApp.RoleModuleFrm.Save(roleId, items, function callback(p) {
                if (p.error) {
                    $.messager.alert('异常消息', '提交保存异常,' + p.error.Message, 'error');
                    return;
                }
                var res = p.value;
                if (res) {
                    var data = res.Data;
                    var msg = res.Msg;
                    if (msg)
                        $.messager.alert('保存异常', msg, 'error');
                    else {
                        if (data == true) {
                            $.messager.alert('消息', '已保存!', 'info');
                        }
                        else
                            $.messager.alert('保存失败', msg, 'error');
                    }
                }
            });
        }
    </script>

</head>
<body>
    <form id="form1" runat="server">
    </form>
    <form id="fm">
    <div class="easyui-panel" title="当前角色" style="width: 800px; height: 80px; padding: 10px;">
        选择角色：<input class="easyui-combobox" id="eRole" data-options="valueField:'Value',textField:'Name',onSelect:onRoleSelect"
            style="width: 150px;" required="true">
        <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'" id="btnSave"
            onclick="saveData()">保存</a></div>
    <div class="easyui-panel" title="分配菜单权限" style="width: 800px; height: 600px; padding: 10px;">
        <div class="easyui-layout" data-options="fit:true">
            <div data-options="region:'west',split:true" title="未选菜单" style="width: 330px; padding: 10px">
                <select id="sLeft" multiple="multiple" style="height: 480px; width: 230px">
                </select>
            </div>
            <div data-options="region:'center'" style="padding: 10px">
                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-undo'" onclick="moveListItem('sRight','sLeft')">
                    左移</a>
                <br />
                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-redo'" onclick="moveListItem('sLeft','sRight')">
                    右移</a>
            </div>
            <div data-options="region:'east'" title="已选菜单" style="width: 330px; padding: 10px">
                <select id="sRight" multiple="multiple" style="height: 480px; width: 230px">
                </select>
            </div>
        </div>
    </div>
    </form>
</body>
</html>
